﻿using System;
using System.ComponentModel;
using System.Web.UI;
using System.Collections;
using System.Collections.Generic;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.Core;
using System.Web;

namespace Yuanlin.Business
{
    public class VisitedCompanyInfo : YuanlinUserControl,
        IReplyConverter<VisitCompanyReply, GetListOperation>
    {
        protected Repeater list;
        protected HtmlControl divGreenNumber;
        protected HtmlControl moreDiv;
        private EntityList visitCompanys;
        private YuanlinUser user = HttpContext.Current.User as YuanlinUser;
        private int entityId = UrlManager.Instance.GetParameterValue<int>(YuanlinParameterName.VisitCompanyId);
        private int categoryId = UrlManager.Instance.GetParameterValue<int>(YuanlinParameterName.CategoryId);
        private string tradeType = UrlManager.Instance.GetParameterValue<string>(YuanlinParameterName.TradeType);
        public string greenNum = UrlManager.Instance.GetParameterValue<string>(YuanlinParameterName.GreenNum);

        public override void GetRequests(List<BaseRequestBinder> requestBinders)
        {
            requestBinders.Add(RequestHelper.BuildGetVisitCompanyListRequest(this, UserIdFromGreenNum, entityId, categoryId, tradeType == null ? "-1" : tradeType,0,6));
        }

        protected override void OnLoad(EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                if (user.IsAnonymous || user.EntityId.Value != UserIdFromGreenNum)
                {
                    divGreenNumber.Visible = false;
                    return;
                }
            }
        }

        private void BindData()
        {
            if (list == null)
            {
                return;
            }
            EntityList temp = new EntityList()  ;
            if (visitCompanys.Count <= 5)
            {
                temp = visitCompanys;
            }
            else
            {
                for (int i = 0; i < 5; i++)
                {
                    temp.Add(visitCompanys[i]);
                }
                moreDiv.Visible = true;
            }
            list.DataSource = temp;
            list.DataBind();
        }


        #region IReplyConverter<VisitCompanyReply,GetListOperation> Members

        public void ConvertReply(VisitCompanyReply reply, GetListOperation operation)
        {
            if (reply != null)
            {
                visitCompanys = reply.EntityList;
                if (!Page.IsPostBack)
                {
                    BindData();
                }
            }
        }

        #endregion
    }
}
